package ytdp.careagle.com.ytdp.net;


import android.util.Log;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.G;

/**
 * Title:OkHttp3Utils
 * Package:com.tomcat360.net
 * Description:TODO
 * Author: llj@tomcat360.com
 * Date: 2017/3/27
 * Version: V1.0.0
 * 版本号修改日期修改人修改内容
 */

public class OkHttp3Utils {
    private static OkHttpClient mOkHttpClient;
    // 设置缓存目录
    private static File cacheDir = new File(G.getFileCacheDirPath(), "mycache");
    private static Cache cache = new Cache(cacheDir, 10 * 1024 * 1024);//10M

    public static OkHttpClient getOkHttpClient() {
        if (mOkHttpClient == null) {
            HttpLoggingInterceptor interceptor = getInterceptor();
            mOkHttpClient = new OkHttpClient.Builder()
                    .cookieJar(new CookiesManager(APP.getInstance().getApplicationContext()))
                    .addInterceptor(interceptor)
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .cache(cache)
                    .build();
        }
        return mOkHttpClient;
    }

    private static HttpLoggingInterceptor getInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (G.APP_DEBUG) {
                    Log.e("tag", message);
                }
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); // 测试
        return interceptor;
    }

}
